var draw;//定义全局变量，当绘图方式改变时删除当前的绘制工具
$(function () {
        //新建底图，瓦片图层
        var raster = new ol.layer.Tile({
            source: new ol.source.MapQuest({
                layer: 'sat'
            })
        });
        //临时图层的数据源
        var source = new ol.source.Vector();
        //新建临时图层，并设置临时图层渲染各种要素的样式
        var vector = new ol.layer.Vector({
            source: source,
            style: new ol.style.Style({
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, .2)'
                }),
                stroke: new ol.style.Stroke({
                    color: '#fc3',
                    width: 2
                }),
                image: new ol.style.Circle({
                    radius: 7,
                    fill: new ol.style.Fill({
                        color: '#fc3'
                    })
                })
            })
        });
        // 新建地图
        var map = new ol.Map({
            layers: [raster, vector],
            target: 'map',
            view: new ol.View({
                center: [-11000000, 4600000],
                zoom: 4
            })
        });
        var typeSelected = document.getElementById('drawtype');
        //初始化绘图工具
        function addInteraction() {
            var value = typeSelected.value;
            if(value !== 'None') {
                draw = new ol.interaction.Draw({
                   source: source,//设置要素愿，绘制结束后将绘制的要素添加到临时图层
                    type: (value)// 绘制的类型
                });
                map.addInteraction(draw);
            }
        }
        //绘制方式改变后重新初始化绘图工具
        typeSelected.onchange = function(e) {
            map.removeInteraction(draw);
            addInteraction();
        }
        var clear = function() {
            source.clear();
        }
        $("#clear").click(clear);
    }
);



